package com.changpay.zywy.ui.activity.count

import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import kotlinx.android.synthetic.main.activity_add_shop_count.*
import kotlinx.android.synthetic.main.title_layout.*


class CountAddShopActivity : BaseActivity(), IView, CountAddShopView.CountChartAddShopClose {

    private var type: String? = null

    override fun attachLayoutRes(): Int {
        return R.layout.activity_add_shop_count
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "新增统计"
        type = intent.getStringExtra("type")
        iv_back.setOnClickListener { finish() }
        count_add_shop.setFragmentManager(supportFragmentManager)
        count_add_shop.setListener(this)
        if (this.type == "shop") {
            count_add_shop.setTitle("新增商户","新增商户","总商户")
        } else if (this.type == "member") {
            count_add_shop.setTitle("新增渠道","新增渠道","总渠道")
        }
    }

    override fun start() {
        count_add_shop.count_add_shop_rg.check(R.id.day_rb)
    }

    override fun loadActionMonthCharData(type: Int, date: String?) {
        if (date != null) {
            loadDatas(type, date)
        }
    }


    fun loadDatas(type: Int, data: String) {
        val param = hashMapOf<String, String>()
        val split = data.split("|")
        param["key"] = token
        param["gc_id"] = intent.getStringExtra("gc_id")
        param["search_date_star"] = split[0]
        param["search_date_end"] = split[1]
        when (type) {
            0, 3 -> param["date_type"] = "day"
            1, 4 -> param["date_type"] = "week"
            2, 5 -> param["date_type"] = "month"
        }
        if (this.type == "shop") {
            RetrofitHelper.service.get_merchant_add(param).sss(this)
            {
                val num = it.result.num
                val num_total = it.result.num_total
                count_add_shop.setData(num, num_total)
            }
        } else if (this.type == "member") {
            RetrofitHelper.service.get_channel_add(param).sss(this)
            {
                val num = it.result.num
                val num_total = it.result.num_total
                count_add_shop.setData(num, num_total)
            }
        }

    }
}
